var tools={
    getStyle:function(element,attr){
        if(window.getComputedStyle){
            return window.getComputedStyle(element)[attr];
        }else{
            if(isNaN(element.currentStyle[attr]) && attr=='opacity'){
                return 0;
            }
            return element.currentStyle[attr];
        }
    },
    bind:function (element,eventName,fn,sign) {
        sign=sign || false;
        //如果是undefined,就相当于是false。0也相当于false
        if(element.addEventListener){
            //DOM 2
            element.addEventListener(eventName,fn,sign);
        }else if(element.attachEvent){
            //IE
            element.attachEvent('on'+eventName,function(){
                fn.apply(element,arguments);
            });
        }else{
            //DOM 0
            element['on'+eventName]=fn;
        }
    },
    unbind:function (element,eventName,fn) {
            //DOM 2
        if(element.removeEventListener){
            element.removeEventListener(eventName,fn);
        }else if(element.detachEvent){
            // IE
            element.detachEvent('on'+eventName,fn);
        }else{
            //DOM 0
            element['on'+eventName]=null;//掏空它
        }
    },
    animate:function (element,attr,callback) {
        //先停止掉之前可能还没结束的定时器(针对的是同一个元素)
        clearInterval(element.timer);
        //给每个元素都赋予一个属于它自己的定时器,不会影响别的元素了
        element.timer=setInterval(function () {
            var operator=true;//默认就当成已经完成了
            for(var p in attr){
                var current;
                if(p==='opacity'){
                    current=tools.getStyle(element,p)*100;
                }else{
                    current=parseInt(tools.getStyle(element,p));
                }
                var speed=(attr[p]-current)/10;//推荐10
                speed=speed>0?Math.ceil(speed):Math.floor(speed);
                current+=speed;
                if((current>=attr[p] && speed>0) || (current<=attr[p] && speed<0)){
                    current=attr[p];
                }else{
                    operator=false;
                }

                if(p==='opacity'){
                    element.style[p]=current/100;
                    element.style.filter = 'alpha(opacity = ' + current + ')';
                }else{
                    element.style[p]=current+'px';
                }

                if(operator){
                    clearInterval(element.timer);
                    callback && callback();
                }
            }
        },20);
    },
    $:function (id) {
        return document.getElementById(id);
    },
    setCookie:function (key,value,day) {
        //如果没有传 天数、则无需生成未来时间，保存期限为当前窗口
        if(day==undefined){
            document.cookie=key+'='+value;
        }else{
            var mydate=new Date();
            var future=mydate.getDate()+day;
            mydate.setDate(future);
            document.cookie=key+'='+value+';expires='+mydate.toGMTString();
        }
    },
    removeCookie:function (key) {
        document.cookie=key+'=xxx;expires='+new Date(0);
    },
    getCookie:function (key) {
        var str=document.cookie;
        if(str==="")return "";
        var arr1=str.split(';');
        for(var i in arr1){
            var arr2=arr1[i].split('=');
            if(arr2[0]===key){
                return arr2[1];
            }
        }
        return "";
    },
    hide(element){
        element.style.display='none';
    },
    show(element){
        element.style.display='block';
    },
    /**
     * @param method    提交方式
     * @param url       提交地址
     * @param data      提交数据
     * @param success   成功回调函数
     * @param fail      失败回调函数
     */
    ajax:function (method,url,data,success,fail) {
        //1.创建XMLHttpRequest
        var ajax=new XMLHttpRequest();
        if(method.toUpperCase()==="POST"){
            ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            //2.打开连接
            ajax.open(method,url);
            ajax.send(data);//POST请求通过send发送数据
        }else{
            ajax.open(method,url+'?'+data);
            //3.发送
            ajax.send();
        }
        //4.监听onreadystatechange
        ajax.onreadystatechange=function () {
            if(this.readyState===4) {
                //6.判断结果是不是200
                if (this.status == 200) {
                    success(this.responseText);
                }else{
                    fail(this.status);
                }
            }
        }
    }
}